package page.sample.administratorpackage;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import dao.impl.AdministratorDao;
import dao.impl.UserDao;
import page.sample.AuthorizationPage;
import entity.Administrator;
import entity.User;

public class ShowUsers extends WebPage {

	public ShowUsers(final Administrator administrator) {

		final UserDao userDao = new UserDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);
		final AdministratorDao administratorDao = new AdministratorDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);

		add(new DataView<User>("table-rows", new ListDataProvider<User>(
				userDao.getAll())) {
			@Override
			protected void populateItem(final Item<User> item) {
				final User user = item.getModelObject();
				// add data for table cell in row
				item.add(new Label("login", user.getLogin()));
				item.add(new Label("password", user.getPassword()));
				item.add(new Label("name", user.getName()));
				item.add(new Label("lastname", user.getLastname()));

				// add button component to open "edit" page
				item.add(new Link("addBlackList") {
					@Override
					public void onClick() {
						User userTemp = new User();
						userTemp = userDao.get(user.getLogin());
						userTemp.setAccess(false);

						if (administrator.getBlackList().size() == 0) {
							administrator.getBlackList().add(userTemp);
						} else {
							
							int count = 0;
							
							for (final User row : administrator.getBlackList()) {
								if (row.getLogin().equals(userTemp.getLogin())) {
									count++;
									break;
								}
							}
							
							if(count == 0) administrator.getBlackList().add(userTemp);
						}
						userDao.update(userTemp);
						administratorDao.update(administrator);
						setResponsePage(new ShowUsers(administrator));
					}
				});
			}
		});

		add(new Link("showUsers") {

			@Override
			public void onClick() {
				setResponsePage(new ShowUsers(administrator));
			}

		});

		add(new Link("showBlackList") {

			@Override
			public void onClick() {
				setResponsePage(new BlackListPage(administrator));
			}

		});
		
		add(new Link("showGoods")
		{

			@Override
			public void onClick() {
				setResponsePage(new ShowGoodsPage(administrator));
				
			}
			
		});
		
		add(new Link("addGoods")
		{

			@Override
			public void onClick() {
				setResponsePage(new AddGoodsPage(administrator));
				
			}
			
		});

		add(new Link("exit") {

			@Override
			public void onClick() {
				setResponsePage(new AuthorizationPage());

			}

		});
	}

}
